import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();

}

class _MyHomePageState extends State<MyHomePage> {
  static final String METHOD_CHANNEL = "com.mx.print/method_channel";
  List<String> msg = List();
  MethodChannel methodChannel;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    methodChannel = new MethodChannel(METHOD_CHANNEL);
    methodChannel.setMethodCallHandler(platformCallHandler);
  }

  Future<dynamic> platformCallHandler(MethodCall call) async {
    switch (call.method) {
      case "device_scan":
        setState(() {
          msg.add(call.arguments);
        });
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        children: <Widget>[
          ListView.builder(
              shrinkWrap: true,
              itemCount: msg.length,
              itemBuilder: (BuildContext context, int index) {
                return _deviceScanString(index);
              }),
          ListView.builder(
              shrinkWrap: true,
              itemCount: msg.length,
              itemBuilder: (BuildContext context, int index) {
                return _deviceScanString(index);
              })
        ],
      ),
    );
  }

  Widget _deviceScanString(int index) {
    return Container(
      width: MediaQuery.of(context).size.width,
      padding: EdgeInsets.all(10.0),
      child: Text(msg[index]),
    );
  }
}
